package com.zq.practice;

import java.util.*;

/**
 * @ProjectName: ybxy6
 * @Titile: StudentMangeSystem
 * @Author: zqsfg
 * @Description: TODO
 */
public class StudentMangeSystem {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        students.add(new Student("1","zq",18,"宜宾"));
        students.add(new Student("2","zxc",19,"荆州"));
        students.add(new Student("3","ysq",20,"北京"));
        students.add(new Student("4","zxy",21,"上海"));
        students.add(new Student("5","zq",18,"四川"));
        students.add(new Student("6","ss",18,"武汉"));
        students.add(new Student("7","mm",18,"北京"));
        students.add(new Student("8","aa",18,"西安"));
        students.add(new Student("9","dc",18,"上海"));
        students.add(new Student("10","ad",18,"南充"));
        students.add(new Student("11","mj",18,"上海"));

        Scanner scanner = new Scanner(System.in);
        int choice;
        //利用循环返回主界面
        do {
            // 显示菜单
            System.out.println("\n学生管理系统");
            System.out.println("1. 添加学生");
            System.out.println("2. 删除学生");
            System.out.println("3. 修改学生信息");
            System.out.println("4. 查询学生");
            System.out.println("5. 退出");
            System.out.print("请输入你的选择: ");
            choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:
                    System.out.println("1. 添加学生");
                    addStudent(students);
                    break;
                case 2:
                    System.out.println("2. 删除学生");
                    deleteStudent(students);
                    break;
                case 3:
                    System.out.println("3. 修改学生信息");
                    updateStudent(students);
                    break;
                case 4:
                    System.out.println("4. 查询学生");
                    queryStudent(students);
                    break;
                case 5:
                    System.out.println("5. 退出");
                    System.out.println("感谢你的使用，再见");
                    break;
                default:
                    System.out.println("输入错误，请重新输入");
            }
        } while (choice != 5);
    }
    //  添加学生
    public static void addStudent(List<Student> students) {
        //创建学生集合
        System.out.println("请输入学生信息：");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学号：");
        String id= scanner.next();
        //判断学号是否存在(学号具有唯一性)
     for (Student student : students) {
         if (student.getId().equals(id)) {
             System.out.println("学号已存在，请重新输入");
             return;
         }
     }
     //输入新的信息
        System.out.println("请输入姓名：");
        String name = scanner.next();
        System.out.println("请输入年龄");
        int age = scanner.nextInt();
        System.out.println("请输入居住地");
        String address = scanner.next();
        Student s = new Student(id,name,age,address);
        //添加数据到集合中
        students.add(s);
        System.out.println("添加成功");
    }
    //  删除学生
    public static void deleteStudent(List<Student> students) {
        Scanner scanner = new Scanner(System.in);
        //判断信息是否为空
        if (students.isEmpty()) {
            System.out.println("没有学生信息，请先添加学生信息");
            return;
        }
        System.out.println("请输入要删除学生的学号：");
        String id = scanner.next();
        //  遍历集合，找到目标学生
        for (Student student : students) {
            if (student.getId().equals(id)) {
                students.remove(student);
                System.out.println("删除成功");
                return;
            }
        }
        System.out.println("没有此学生");
    }
    // 修改学生信息
    public static void updateStudent(List<Student> students) {
        Scanner scanner = new Scanner(System.in);

        // 判断信息是否为空
        if (students.isEmpty()) {
            System.out.println("没有学生信息，请先添加学生信息");
            return;
        }

        System.out.println("请输入要修改学生的学号：");
        String id = scanner.next();
        //  遍历集合，找到目标学生
        Student targetStudent = null;
        for (Student student : students) {
            if (student.getId().equals(id)) {
                // 找到目标学生
                targetStudent = student;
                break;
            }
        }
// 检查目标学生是否为空
        if (targetStudent == null) {
            System.out.println("没有此学生");
            return;
        }

        System.out.println("当前学生信息：");
        System.out.println(targetStudent); // 显示原信息

        // 保存原始学号用于比较
        String originalId = targetStudent.getId();

        // 输入新的信息
        System.out.print("请输入新姓名（原姓名：" + targetStudent.getName() + "）: ");
        targetStudent.setName(scanner.next());

        System.out.print("请输入新年龄（原年龄：" + targetStudent.getAge() + "）: ");
        targetStudent.setAge(scanner.nextInt());

        System.out.print("请输入新住址（原住址：" + targetStudent.getAddress() + "）: ");
        targetStudent.setAddress(scanner.next());

        // 学号修改，需要检查新学号是否已存在
        String newId;
        boolean isValidId;
        do {
            System.out.print("请输入新的学号（原学号：" + targetStudent.getId() + "）: ");
            newId = scanner.next();
            isValidId = true;

            // 如果新学号和原学号相同，直接通过验证
            if (newId.equals(originalId)) {
                break;
            }

            // 检查新学号是否已存在
            for (Student student : students) {
                if (student.getId().equals(newId)) {
                    System.out.println("学号已存在，请重新输入");
                    isValidId = false;
                    break;
                }
            }
        } while (!isValidId);

        // 设置新学号
        targetStudent.setId(newId);
        System.out.println("学生信息修改成功！");
    }


    //  查询学生
    public static void queryStudent(List<Student> students) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("\n查询方式选择：");
        System.out.println("1. 按学号查询单个学生");
        System.out.println("2. 查看所有学生信息");
        System.out.print("请选择查询方式（1/2）: ");
        int Choice = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符
        //按学号查询单个学生
        if (Choice == 1) {
            System.out.print("请输入要查询的学号: ");
            String id = scanner.nextLine();
          for (Student student : students){
              if (student.getId().equals(id)) {
                  System.out.println("查询结果：");
                  System.out.println("学号\t姓名\t年龄\t住址");
                  System.out.printf("%s\t%s\t%d\t%s%n",
                          student.getId(), student.getName(), student.getAge(), student.getAddress());
                  return;
              }
          }
          System.out.println("没有此学生");

        }
        //查看所有学生信息
        else if (Choice == 2) {
            if (students.size() == 0){
                System.out.println("没有学生信息");
                return;
            }
//             创建学生列表副本并进行正序排序
            List<Student> sortedStudents = new ArrayList<>(students);
            Collections.sort(sortedStudents);
            Collections.sort(sortedStudents, (s1, s2) -> {
                try {
                    // 强转为数字比较
                    int num1 = Integer.parseInt(s1.getId());
                    int num2 = Integer.parseInt(s2.getId());
                    return Integer.compare(num1, num2);
                } catch (NumberFormatException e) {
                    // 若不是数字则按字符串比较
                    return s1.getId().compareTo(s2.getId());
                }
            });


            //输出
            System.out.println("\n所有学生信息列表（按学号正序排列）：");
            System.out.println("学号\t姓名\t年龄\t住址");
            System.out.println("--------------------");

            for (Student student : sortedStudents) {
                System.out.printf("%s\t%s\t%d\t%s%n",
                        student.getId(), student.getName(), student.getAge(), student.getAddress());
            }



        }
        else {
            System.out.println("输入错误，请重新输入");
        }


    }

}
